package cn.nexd.app.indoor.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

/**
 * Created by xun on 2016/3/23.
 */
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    private String[] emailSufixs = new String[]{"@nexdtech.com",
    };

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        this.setAdapter(new EmailAutoCompleteAdapter(context, android.R.layout.simple_list_item_1, emailSufixs));
        this.setThreshold(1);
        this.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String emailAddress = CustomAutoCompleteTextView.this.getText().toString();
                    if (!TextUtils.isEmpty(emailAddress)) {
                        performFiltering(emailAddress, 0);
                    }
                }
            }
        });
    }

    @Override
    protected void replaceText(CharSequence text) {
        String emailAddress = this.getText().toString();
        int index = emailAddress.indexOf("@");
        if (index != -1)
            emailAddress = emailAddress.substring(0, index);
        super.replaceText(emailAddress + text);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {
        String emailAddress = text.toString();
        int index = emailAddress.indexOf("@");
        if (index != -1) {
            super.performFiltering(emailAddress.substring(index), keyCode);
        }

    }

    private class EmailAutoCompleteAdapter extends ArrayAdapter<String> {
        public EmailAutoCompleteAdapter(Context context, int textResourceId, String[] email_s) {
            super(context, textResourceId, email_s);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null);
                TextView tv_autoEdit = (TextView) convertView.findViewById(android.R.id.text1);
                StringBuffer sbuffer = new StringBuffer();
                String emailtext = CustomAutoCompleteTextView.this.getText().toString();
                int emailIndex = emailtext.indexOf("@");
                if (emailIndex != -1) {
                    emailtext = emailtext.substring(0, emailIndex);
                    sbuffer.append(emailtext + getItem(position));
                    tv_autoEdit.setText(sbuffer.toString());
                }
            }
            return convertView;
        }
    }
}
